package com.zijie.shoprbacapi.service;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.rbac.model.Perm;
import com.zijie.rbac.model.Role;
import com.zijie.rbac.service.RoleService;
import com.zijie.shoprbacapi.mapper.RoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

//@CacheConfig(cacheNames = "c.s.r.service.impl")
@Service
public class RoleServiceImpl implements RoleService {
    private RoleDao roleDao;

    @Autowired
    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    @Override
    public Page<Role> findAll(Role role, Page<Role> page) {
        return roleDao.findAll(role, page);
    }

//    @Cacheable(key = "#a0")
    @Override
    public Role findById(Integer id) {
        return roleDao.selectById(id);
    }

//    @CacheEvict(allEntries = true)
    @Override
    public boolean save(Role role) {
        return roleDao.insert(role) > 0;
    }

//    @CacheEvict(allEntries = true)
    @Override
    public boolean update(Role role) {
        return roleDao.updateById(role) > 0;
    }

//    @CacheEvict(allEntries = true)
    @Override
    public int deleteByIds(List<Integer> ids) {
        return roleDao.deleteByIds(ids);
    }

    @Override
    public boolean assignAdminToGroup(Long adminId, Long userGroupId) {
        return roleDao.assignAdminToGroup(adminId, userGroupId);
    }

    @Override
    public boolean assignPermToRole(Long roleId, Long permId) {
        return roleDao.assignPermToRole(roleId, permId);
    }

    @Override
    public List<Perm> findPermsByRoleId(Long roleId) {
        return roleDao.findPermsByRoleId(roleId);
    }

    @Override
    public boolean removeRolePermission(Long roleId, Long permissionId) {
        return roleDao.removeRolePermission(roleId, permissionId);
    }
}
